import java.util.Objects;
import java.util.Stack;

/**
 * https://leetcode-cn.com/problems/backspace-string-compare/
 *
 * 844. 比较含退格的字符串
 */

public class _844_BackspaceCompare {


    public boolean backspaceCompare(String S, String T) {
        if (null == S || null == T) {
            return false;
        }
        Stack<Character> ST = new Stack();
        Stack<Character> TT = new Stack();

        char[] sa = S.toCharArray();
        char[] ta = T.toCharArray();
        for (char c : sa) {
            if (!('#' == c)) {
                ST.push(c);
            } else if (!ST.empty()) {
                ST.pop();
            }
        }

        for (char c : ta) {
            if (!('#' == c)) {
                TT.push(c);
            } else if (!TT.empty()) {
                TT.pop();
            }
        }
        return Objects.equals(ST.toString(), TT.toString());
    }
}